package com.zhao.dota.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.dota.model.DotaMatch;
import com.zhao.dota.service.MatchPlayerService;
import com.zhao.dota.service.MatchService;
import com.zhao.dota.service.ReportService;
import com.zhao.dota.struct.MonthMatches;
import com.zhao.dota.struct.Result;
import com.zhao.dota.vo.HeroWinRate;
import com.zhao.dota.vo.Statistical;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Resource
    private ReportService reportService;
    @Resource
    private MatchService matchService;
    @Resource
    private MatchPlayerService matchPlayerService;

    @GetMapping("/current")
    public Result<Statistical> current() {
        YearMonth yearMonth = YearMonth.now();
        List<DotaMatch> monthMatch = matchService.getNormalMonthMatch(yearMonth);
        Statistical statistical = reportService.generateMonthlyReport(yearMonth, monthMatch);
        return Result.success(statistical);
    }

    @GetMapping("/")
    public Result<Statistical> report(YearMonth yearMonth) {
        List<DotaMatch> monthMatch = matchService.getNormalMonthMatch(yearMonth);
        Statistical statistical = reportService.generateMonthlyReport(yearMonth, monthMatch);
        return Result.success(statistical);
    }

    @GetMapping("/months")
    public Result<List<String>> months() {
        List<MonthMatches> months = matchService.monthMatches();
        List<String> collect = months.stream().filter(s -> s.getCount() >= 10).map(MonthMatches::getMonth).collect(Collectors.toList());
        return Result.success(collect);
    }

    @GetMapping("/heroes")
    public Result<Page<HeroWinRate>> hero(Page page, YearMonth yearMonth) {
        LocalDateTime startTime = yearMonth.atDay(1).atTime(6, 0);
        LocalDateTime endTime = yearMonth.plusMonths(1).atDay(1).atTime(6, 0);
        Page<HeroWinRate> heroWinRatePage = matchPlayerService.groupByHero(page, startTime, endTime);
        heroWinRatePage.getRecords().forEach(HeroWinRate::setHeroAvatar);
        return Result.success(heroWinRatePage);
    }

}
